package com.yuanrui.interview.server.service.impl;

import com.alibaba.nacos.shaded.com.google.common.base.Preconditions;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanrui.interview.api.req.InterviewReq;
import com.yuanrui.interview.api.vo.InterviewVO;
import com.yuanrui.interview.server.dao.SubjectDao;
import com.yuanrui.interview.server.entity.po.InterviewHistory;
import com.yuanrui.interview.server.dao.InterviewHistoryDao;
import com.yuanrui.interview.server.entity.po.SubjectLabel;
import com.yuanrui.interview.server.service.InterviewEngine;
import com.yuanrui.interview.server.service.InterviewHistoryService;
import com.yuanrui.interview.server.util.PDFUtil;
import com.yuanrui.interview.server.util.keyword.KeyWordUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 面试汇总记录表(InterviewHistory)表服务实现类
 *
 * @author makejava
 * @since 2024-07-30 19:30:53
 */
@Service("interviewHistoryService")
public class InterviewHistoryServiceImpl extends ServiceImpl<InterviewHistoryDao,InterviewHistory> implements InterviewHistoryService, ApplicationContextAware {
    private static final Map<String,InterviewEngine> engineMap=new HashMap<>();

    @Resource
    private InterviewHistoryDao interviewHistoryDao;

    @Resource
    private SubjectDao subjectDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public InterviewHistory queryById(Long id) {
        return this.interviewHistoryDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param interviewHistory 实例对象
     * @return 实例对象
     */
    @Override
    public InterviewHistory insert(InterviewHistory interviewHistory) {
        this.interviewHistoryDao.insert(interviewHistory);
        return interviewHistory;
    }

    /**
     * 修改数据
     *
     * @param interviewHistory 实例对象
     * @return 实例对象
     */
    @Override
    public InterviewHistory update(InterviewHistory interviewHistory) {
        this.interviewHistoryDao.update(interviewHistory);
        return this.queryById(interviewHistory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.interviewHistoryDao.deleteById(id) > 0;
    }

    @Override
    public InterviewVO analyse(InterviewReq req) {
        List<String> keyWords = buildKeyWords(req.getUrl());
        InterviewEngine interviewEngine = engineMap.get(req.getEngine());
        Preconditions.checkNotNull(interviewEngine, "未找到对应的面试引擎");
        return interviewEngine.analyse(keyWords);
    }

    private List<String> buildKeyWords(String url) {
        String pdfText= PDFUtil.getPdfText(url);
        if (!KeyWordUtil.isInit()){
            List<String> list= subjectDao.listAllLabel().stream().map(SubjectLabel::getLabelName).toList();
            KeyWordUtil.addWord(list);
        }
        return KeyWordUtil.buildKeyWordsLists(pdfText);
    }

    /**
     * 启动时加载所有面试引擎
     * @ return 面试引擎
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Collection<InterviewEngine> engines = applicationContext.getBeansOfType(InterviewEngine.class).values();
        for (InterviewEngine engine : engines) {
            engineMap.put(engine.engineType().name(), engine);
        }
    }
}
